package com.cheyunkeji.er.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.cheyunkeji.er.utils.ImageLoadUtil;

import java.util.ArrayList;

import androidx.viewpager.widget.PagerAdapter;

/**
 * Created by Michael on 2017/2/17 11:10.
 */

public class ViewPagerAdapter extends PagerAdapter {

    private ArrayList<String> imgUrlSet;
    private Context context ;

    public ViewPagerAdapter(ArrayList<String> imgUrlSet, Context context){
        this.imgUrlSet = imgUrlSet ;
        this.context = context;
    }


    @Override
    public int getCount() {
        if(imgUrlSet.size() > 1)
            return Integer.MAX_VALUE;
        else
            return imgUrlSet.size() ;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        ImageView imageView = new ImageView(context);
        if(imgUrlSet!=null && imgUrlSet.size() > 0 && imgUrlSet.size()>position % imgUrlSet.size()) {
            ImageLoadUtil.loadImg(context,imgUrlSet.get(position%imgUrlSet.size()),imageView);
        }
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);
    }
}
